package com.liusand.admin.controller;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.jwt.JWT;
import com.liusand.admin.service.AdminService;
import com.liusand.xo.common.Result;
import com.liusand.xo.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author LiuSha
 */
@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;


    @PostMapping("login")
    public Result login(@RequestBody Admin admin){

        Admin adminInDb = adminService.findByEmail(admin.getEmail());
        if(adminInDb == null){
            return Result.error();
        }


        if(!adminInDb.getPassword().equals(SecureUtil.md5(admin.getPassword()))){
            return Result.error();
        }


        String token = JWT.create()
                .setPayload("adminId",admin.getAdminId())
                .setPayload("email",admin.getEmail())
                .setKey("ASDFGHJKL".getBytes())
                .sign();
        return Result.success(token);
    }
}
